package com.chixing.controller;


import com.chixing.entity.CustomerCoupon;
import com.chixing.entity.LoginCustomer;
import com.chixing.service.impl.CouponAllServiceImpl;
import com.chixing.service.impl.CustomerCouponServiceImpl;
import com.chixing.util.Result;
import com.chixing.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shy
 * @since 2022-10-20
 */
@Controller
@RequestMapping("/customercoupon")
public class CustomerCouponController {
    @Autowired
    private CustomerCouponServiceImpl customerCouponService;

    @GetMapping("/getall")

    public ModelAndView getValidCoupon(String token) {
        System.out.println("123456");
       LoginCustomer loginCustomer = TokenUtil.parseToken(token);

        List<CustomerCoupon> validCoupon = customerCouponService.getAllCouponsByUserId(loginCustomer.getCustId());
        System.out.println("我的优惠券:"+ validCoupon);
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("couponList", validCoupon);
        modelAndView.setViewName("/customer/personalCenter-DiscountCode");
//        modelAndView.setViewName("customer/personalCenter-DiscountCode");
        return modelAndView;

    }

    @GetMapping("/couponList")
    @ResponseBody
    public Result getCouponList(HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println("优惠列表获得token："+token);

        // ajax 请求后端加载数据
        LoginCustomer loginCustomer = TokenUtil.parseToken(token);
        Integer custId = loginCustomer.getCustId();

        List<CustomerCoupon> customerCouponList = customerCouponService.getAllCouponsByUserId(custId);
        if (customerCouponList != null) {
            System.out.println("获得优惠卷列表：" + customerCouponList);
            return Result.success("获得优惠卷列表成功", customerCouponList);
        }else {
            return Result.fail("获得优惠卷列表失败", null);
        }
    }


}
